package com.fingard.dsp.bank.directbank.hbc02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.io.FileHelper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;

/**
 * @version 1.0
 */
public class HBC02Transaction extends HBC02Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        String accountNum = p_req7516.actNum;
        Date dateStart = Format.Date8Format.parse(p_req7516.dateFrom);
        Date dateEnd = Format.Date8Format.parse(p_req7516.dateTo);
        Calendar calendar = Calendar.getInstance();
        while (!dateStart.after(dateEnd)) {
            String dealedPath = getBankConfig().getValueAsString("DealedPath") + File.separator +
                    Format.Date8Format.format(dateStart) + File.separator + "MT940" + File.separator +
                    accountNum + "_7516.txt";
            if (!FileHelper.existFile(dealedPath)) {
                calendar.setTime(dateStart);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                dateStart = calendar.getTime();
                continue;
            }
            String readLine = "";
            FileInputStream fis = null;
            InputStreamReader isr = null;
            BufferedReader reader = null;
            try {
                fis = new FileInputStream(dealedPath);
                isr = new InputStreamReader(fis, "UTF-8");
                reader = new BufferedReader(isr);

                while ((readLine = reader.readLine()) != null) {
                    //账号 | 币种 | 交易日期| 交易时间| 计息日期 | 交易金额 |借贷标识| 账户余额 |对方账号|
//                    对方户名 | 对方银行 |用途|
                    String[] transDetail = readLine.split("\\|");
                    RetAtsTransDetail retDetail = new RetAtsTransDetail();
                    retDetail.actArea = p_req7516.actArea;
                    retDetail.actNum = transDetail[0].trim();
                    retDetail.cur = convertCurToATS(transDetail[1].trim());
                    retDetail.tranDate = transDetail[2].trim();
                    retDetail.tranTime = transDetail[3].trim();
                    retDetail.valueDate = transDetail[4].trim();
                    retDetail.tranAmt = transDetail[5].trim();
                    retDetail.cdFlag = "C".equals(transDetail[6].trim()) ? "2" : "1";
                    retDetail.recAccNum = transDetail[8].trim();
                    retDetail.accBal = transDetail[7].trim();
                    retDetail.recAccName = transDetail[9].trim();
                    retDetail.recAccBank = transDetail[10].trim();
                    String[] transArray = transDetail[11].split("##");
                    if (transArray.length > 1) {
                        if (transArray[0].trim().length() > 256) {
                            retDetail.usage = transArray[0].trim().substring(0, 256);
                            retDetail.comment = transArray[0].trim().substring(256) + " " + transArray[1];
                        } else {
                            retDetail.usage = transArray[0].trim();
                            retDetail.comment = transArray[1];
                        }
                    } else {
                        if (transArray[0].trim().length() > 256) {
                            retDetail.usage = transArray[0].trim().substring(0, 256);
                            retDetail.comment = transArray[0].trim().substring(256);
                        } else {
                            retDetail.usage = transArray[0].trim();
                        }
                    }
                    retDetail.askNumber = transDetail.length > 11 ? transDetail[12].trim() : "";
                    retAts.writeRetDetail(retDetail);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    fis.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (reader != null) {
                    reader.close();
                }
            }
            calendar.setTime(dateStart);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            dateStart = calendar.getTime();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        //BankCode TransType TransSeqID
        RetAtsTrans retAts = new RetAtsTrans(p_req7515.reqHeader);
        //写返回ATS的统一头部信息
        retAts.writeStart();
        String accNum = p_req7515.actNum;
        String date = Format.Date8Format.format(new Date());
        String dealedPath = getBankConfig().getValueAsString("DealedPath") + File.separator + date +
                File.separator + "MT942" + File.separator + accNum + "_7515.txt";
        if (FileHelper.existFile(dealedPath)) {
            FileInputStream fis = null;
            InputStreamReader isr = null;
            BufferedReader reader = null;
            try {
                fis = new FileInputStream(dealedPath);
                isr = new InputStreamReader(fis, "UTF-8");
                reader = new BufferedReader(isr);

                String readLine = "";
                while ((readLine = reader.readLine()) != null) {
                    // 账号 | 币种 | 交易日期| 交易时间| 计息日期 | 交易金额 |借贷标识| 账户余额 |对方账号|
                    // 对方户名 | 对方银行 |用途 |支付流水号
                    String[] transDetail = readLine.split("\\|");
                    RetAtsTransDetail detail = new RetAtsTransDetail();
                    detail.actArea = p_req7515.actArea;
                    detail.actNum = transDetail[0].trim();
                    detail.cur = convertCurToATS(transDetail[1].trim());
                    detail.tranDate = transDetail[2].trim();
                    detail.tranTime = transDetail[3].trim();
                    detail.valueDate = transDetail[4].trim();
                    detail.tranAmt = transDetail[5].trim();
                    detail.cdFlag = "C".equals(transDetail[6].trim()) ? "2" : "1";
                    detail.accBal = transDetail[7].trim();
                    detail.recAccNum = transDetail[8].trim();
                    detail.recAccBank = transDetail[10].trim();
                    detail.recAccName = transDetail[9].trim();
                    String[] transArray = transDetail[11].split("##");
                    if (transArray.length > 1) {
                        if (transArray[0].trim().length() > 256) {
                            detail.usage = transArray[0].trim().substring(0, 256);
                            detail.comment = transArray[0].trim().substring(256) + " " + transArray[1];
                        } else {
                            detail.usage = transArray[0].trim();
                            detail.comment = transArray[1];
                        }
                    } else {
                        if (transArray[0].trim().length() > 256) {
                            detail.usage = transArray[0].trim().substring(0, 256);
                            detail.comment = transArray[0].trim().substring(256);
                        } else {
                            detail.usage = transArray[0].trim();
                        }
                    }
                    detail.askNumber = transDetail.length > 11 ? transDetail[12].trim() : "";
                    retAts.writeRetDetail(detail);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    fis.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (reader != null) {
                    reader.close();
                }
            }
        } else {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("今日明细银行还没返回,请稍后再查");
        }
        retAts.writeEnd();
        return retAts;
    }
}
